﻿/**************************************************************
 *
 * 唯一标识：9b5ffe62-1b15-42ce-9f36-539511816351
 * 命名空间：Sgr.DataCategories.Controllers
 * 创建时间：2023/8/20 16:57:30
 * 机器名称：DESKTOP-S0D075D
 * 创建者：antho
 * 电子邮箱：fengqinhua2016@163.com
 * 描述：
 *
 **************************************************************/

using MediatR;
using Microsoft.AspNetCore.Mvc;
using Sgr.AspNetCore.ActionFilters.AuditLogs;
using Sgr.AspNetCore.ActionFilters.Permissions;
using Sgr.DataCategories.Application.Commands.DataCategoryItem;
using Sgr.DataCategories.Application.Queries;
using Sgr.DataCategories.Application.ViewModels;

namespace Sgr.DataCategories.Controllers
{
    /// <summary>
    /// 数据字典管理接口
    /// </summary>
    [Route("api/v1/sgr/data-categories")]
    [ApiController]
    [Produces("application/json")]
    [Tags("DataCategory")]
    public class DataCategoryController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IDataCategoryQueries _dataCategoryQueries;

        public DataCategoryController(
            IMediator mediator,
            IDataCategoryQueries dataCategoryQueries)
        {
            _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
            _dataCategoryQueries = dataCategoryQueries ?? throw new ArgumentNullException(nameof(dataCategoryQueries));
        }

        #region Commond

        /// <summary>
        /// 创建数据字典项
        /// </summary>
        /// <param name="command">创建数据字典项的参数</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>创建结果</returns>
        /// <response code="200">创建成功</response>
        /// <response code="400">请求参数验证失败</response>
        /// <response code="401">未经授权的访问</response>
        [CheckPermission(Permissions.CreateAndUpdateCategpryItem)]
        [HttpPost]
        [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
        [AuditLogActionFilter("创建数据字典项")]
        public async Task<ActionResult<bool>> CreateAsync([FromBody] CreateCategpryItemCommand command, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(command, cancellationToken);
            return Ok(result);
        }

        /// <summary>
        /// 更新数据字典项
        /// </summary>
        /// <param name="command">更新数据字典项的参数</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>更新结果</returns>
        /// <response code="200">更新成功</response>
        /// <response code="400">请求参数验证失败</response>
        /// <response code="401">未经授权的访问</response>
        [CheckPermission(Permissions.CreateAndUpdateCategpryItem)]
        [HttpPut]
        [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
        [AuditLogActionFilter("修改数据字典项")]
        public async Task<ActionResult<bool>> UpdateAsync([FromBody] UpdateCategpryItemCommand command, CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(command, cancellationToken);
            return !result.IsSuccess ? this.CustomBadRequest(result.Message) : Ok(result.Data);
        }

        /// <summary>
        /// 删除数据字典项
        /// </summary>
        /// <param name="id">要删除的数据字典项ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>删除结果</returns>
        /// <response code="200">删除成功</response>
        /// <response code="401">未经授权的访问</response>
        [CheckPermission(Permissions.DeleteCategpryItem)]
        [HttpDelete("{id:long:min(1)}")]
        [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
        [AuditLogActionFilter("删除数据字典项")]
        public async Task<ActionResult<bool>> DeleteAsync([FromRoute] long id, CancellationToken cancellationToken)
        {
            return Ok(await _mediator.Send(new DeleteCategpryItemCommand { Id = id }, cancellationToken));
        }

        #endregion Commond

        #region Queries

        /// <summary>
        /// 分页查询数据字典项列表
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>分页的数据字典项列表</returns>
        /// <response code="200">查询成功</response>
        /// <response code="401">未经授权的访问</response>
        [CheckPermission(Permissions.QueryCategpryItem)]
        [HttpPost("search")]
        [ProducesResponseType(typeof(IEnumerable<OutDataDictionaryItemModel>), StatusCodes.Status200OK)]
        [AuditLogActionFilter("查询符合条件的数据字典项集合")]
        public async Task<ActionResult<IEnumerable<OutDataDictionaryItemModel>>> SearchAsync(
            [FromBody] InCategpryItemSearchModel request, CancellationToken cancellationToken)
        {
            return Ok(await _dataCategoryQueries.GetItemsByCategoryTypeAsync(request, cancellationToken));
        }

        /// <summary>
        /// 获取数据字典项详情
        /// </summary>
        /// <param name="id">数据字典项ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>数据字典项详情</returns>
        /// <response code="200">获取成功</response>
        /// <response code="401">未经授权的访问</response>
        /// <response code="404">指定的数据字典项不存在</response>
        [CheckPermission(Permissions.QueryCategpryItem)]
        [HttpGet("{id:long:min(1)}")]
        [ProducesResponseType(typeof(OutDataDictionaryItemModel), StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [AuditLogActionFilter("查询数据字典项详情")]
        public async Task<ActionResult<OutDataDictionaryItemModel>> GetAsync([FromRoute] long id, CancellationToken cancellationToken)
        {
            var item = await _dataCategoryQueries.GetItemByIdAsync(id, cancellationToken);
            return item == null ? NotFound() : Ok(item);
        }

        #endregion Queries
    }
}